/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelRemoverPaleteVazio.java
 *
 * Created on 26/09/2011, 17:16:28
 */

package modules.enderecamento.gui.jpanel;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import modules.enderecamento.exception.PaleteNaoEnderecadoException;
import modules.enderecamento.exception.PaleteOcupadoException;
import modules.enderecamento.facade.WMSFacade;
import modules.enderecamento.gui.custom.LimitedIntegerJTextField;
import modules.enderecamento.models.dto.Palete;
import modules.enderecamento.util.BoxPanel;
import modules.enderecamento.util.Funcoes;
import modules.enderecamento.validation.Validation;

/**
 *
 * @author Clezio
 */
public class PanelRemoverPaleteVazio extends javax.swing.JPanel {

    private WMSFacade wmsFacade;
    /** Creates new form PanelRemoverPaleteVazio */
    public PanelRemoverPaleteVazio() {
        initComponents();
        wmsFacade = new WMSFacade();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lbPalete = new modules.enderecamento.gui.custom.WMSLabel();
        tfPalete = new LimitedIntegerJTextField(10);
        lbConfirmar = new modules.enderecamento.gui.custom.WMSLabel();
        tfConfirmar = new modules.enderecamento.gui.custom.WMSJTextField();
        wMSLabel6 = new modules.enderecamento.gui.custom.WMSLabel();
        btnRemoverPalete = new modules.enderecamento.gui.custom.WMSJButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new modules.enderecamento.gui.custom.WMSJTable();

        setBackground(new java.awt.Color(238, 238, 238));
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Remoção de Paletes da Rua");

        lbPalete.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbPalete.setText("Palete:");

        tfPalete.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfPaleteFocusLost(evt);
            }
        });
        tfPalete.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPaleteKeyPressed(evt);
            }
        });

        lbConfirmar.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbConfirmar.setText("Confimar dados:");

        tfConfirmar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConfirmarKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfConfirmarKeyReleased(evt);
            }
        });

        wMSLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        wMSLabel6.setText("(S) ou (N)");

        btnRemoverPalete.setMnemonic('T');
        btnRemoverPalete.setText("Remover");
        btnRemoverPalete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverPaleteActionPerformed(evt);
            }
        });
        btnRemoverPalete.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnRemoverPaleteKeyPressed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Palete", "Rua"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 772, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(lbConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(tfConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10)
                                        .addComponent(wMSLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(btnRemoverPalete, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 544, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbPalete, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(tfPalete, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(105, 105, 105)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbPalete, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfPalete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbConfirmar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tfConfirmar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(wMSLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnRemoverPalete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfPaleteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPaleteFocusLost

}//GEN-LAST:event_tfPaleteFocusLost

    private void tfPaleteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPaleteKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            if (tfPalete.getText().length() > 0) {
                tfConfirmar.requestFocus();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfPalete.setText("");
        }
}//GEN-LAST:event_tfPaleteKeyPressed

    private void tfConfirmarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConfirmarKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            String confirm = tfConfirmar.getText();
            if (confirm.toUpperCase().equals("S")) {
                efetuarRemocao();
            } else if (confirm.toUpperCase().equals("N")) {
                clearFields();
            } else {
                BoxPanel.showMessageDialog(null, "A confirmação é um campo obrigatório.");
                tfConfirmar.setText("");
                tfConfirmar.requestFocus();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfConfirmar.setText("");
            tfPalete.requestFocus();
        }
}//GEN-LAST:event_tfConfirmarKeyPressed

    private void tfConfirmarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConfirmarKeyReleased
        if (tfConfirmar.getText().length() > 0) {
            tfConfirmar.setText(tfConfirmar.getText().substring(0, 1));
        }
}//GEN-LAST:event_tfConfirmarKeyReleased

    private void btnRemoverPaleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverPaleteActionPerformed
        efetuarRemocao();
}//GEN-LAST:event_btnRemoverPaleteActionPerformed

    private void btnRemoverPaleteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnRemoverPaleteKeyPressed

}//GEN-LAST:event_btnRemoverPaleteKeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked

}//GEN-LAST:event_jTable1MouseClicked

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        clearFields();
        listarPaletesVazios(
                                wmsFacade.getListPaleteEnderecadoVazio()
                        );
    }//GEN-LAST:event_formAncestorAdded


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private modules.enderecamento.gui.custom.WMSJButton btnRemoverPalete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private modules.enderecamento.gui.custom.WMSJTable jTable1;
    private modules.enderecamento.gui.custom.WMSLabel lbConfirmar;
    private modules.enderecamento.gui.custom.WMSLabel lbPalete;
    private modules.enderecamento.gui.custom.WMSJTextField tfConfirmar;
    private modules.enderecamento.gui.custom.WMSJTextField tfPalete;
    private modules.enderecamento.gui.custom.WMSLabel wMSLabel6;
    // End of variables declaration//GEN-END:variables

    private void efetuarRemocao() {
        Palete palete = null;
        if(tfPalete.getText().length() == 0){
            Validation.getInstance().addErros("palete", "Palete é um campo obrigatório.");
        }
        if(Validation.getInstance().hasErrors()){
            BoxPanel.showMessageError(Funcoes.getListErros(Validation.getInstance().getErros()));
            return;
        }
        palete = wmsFacade.obterPaleteById(Integer.parseInt(tfPalete.getText()));
        if(palete == null){
            tfConfirmar.setText("");
            tfPalete.requestFocus();
            BoxPanel.showMessageDialog(null, "Palete não encontrado.");
            return;
        }

        try {
            if (wmsFacade.removerPaleteVazio(palete)) {
                BoxPanel.showMessageDialog(null, "Operaçao realizada com sucesso.");
                clearFields();
                listarPaletesVazios(wmsFacade.getListPaleteEnderecadoVazio());
            }
        } catch (PaleteNaoEnderecadoException ex) {
            tfConfirmar.setText("");
            tfPalete.requestFocus();
            BoxPanel.showMessageDialog(null, ex.getMessage());
        } catch (PaleteOcupadoException ex) {
            tfConfirmar.setText("");
            tfPalete.requestFocus();
            BoxPanel.showMessageDialog(null, ex.getMessage());
        }
    }

    private void clearFields() {
        tfPalete.setText("");
        tfConfirmar.setText("");
        tfPalete.requestFocus();
    }

    private void listarPaletesVazios(List<Palete> paletes) {
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        modelo.setNumRows(0);
        if(paletes == null){
            return;
        }

        for (int i = 0; i < paletes.size(); i++) {
            Integer paleteId = paletes.get(i).getId();
            String ruaName = paletes.get(i).getRua().getNome();
            modelo.addRow(new Object[]{
                        paleteId, ruaName
                    });
        }
    }

}
